package ihm;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JPasswordField;

import network.Connexion;
import network.SocketUtilisateur;

import data.Authentification;
import data.Utilisateur;

public class FenetreAuthentifier extends JFrame  {

	private JPanel contentPane;
	private JTextField txtIdentifiant;
	private JPasswordField pwMotDePasse;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FenetreAuthentifier frame = new FenetreAuthentifier(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FenetreAuthentifier(final Connexion con, final Authentification auth) {
		super();
		/*
		if(! con.getState()){
			System.err.println("Authentification imposible");
			return;
		}
		*/
		
		setTitle("Murder Party - S'authentifier");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 400, 164);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblIdentifiant = new JLabel("Identifiant :");
		
		JLabel lblMotDePasse = new JLabel("Mot de passe :");
		
		txtIdentifiant = new JTextField();
		txtIdentifiant.setColumns(10);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JButton btnValider = new JButton("Valider");
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String strMotDePasse = new String(pwMotDePasse.getPassword());
				//authentifie = Utilisateur.sauthentifier(txtIdentifiant.getText(), strMotDePasse);
				Utilisateur u = new Utilisateur(txtIdentifiant.getText(),strMotDePasse,false,0);
				if (txtIdentifiant.getText().length()!=0)
				{
					if (pwMotDePasse.getPassword().length !=0 )
					{
						dispose();
						con.send(u);
					
					}
					else
					{
						JOptionPane.showMessageDialog(null,"Veuillez choisir un mot de passe");	
					}
				}
				else
				{
					JOptionPane.showMessageDialog(null,"Veuillez choisir un pseudo");
				}
				System.out.println("[Client][FenetreAuth] Demande user:"+u.getPseudoUtilisateur()+" pass:"+u.getPasswordUtilisateur());
				// on envoi l'utilisateur
				
				
				
				/* Pour Thiery
				int count=0;
				while(!auth.getState() && count < 10){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					count++;
				}
				
				if(count < 10){
					dispose();
				}
				*/
				
				
				
				
				
				
			
			}
		});
		
		pwMotDePasse = new JPasswordField();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblIdentifiant)
						.addComponent(lblMotDePasse))
					.addGap(26)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(pwMotDePasse)
						.addComponent(txtIdentifiant, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
					.addContainerGap(63, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(212, Short.MAX_VALUE)
					.addComponent(btnValider)
					.addGap(18)
					.addComponent(btnAnnuler)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblIdentifiant)
						.addComponent(txtIdentifiant, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMotDePasse)
						.addComponent(pwMotDePasse, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAnnuler)
						.addComponent(btnValider)))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
